<?php
namespace App\Admin\Exceptions\Exports;

use Encore\Admin\Grid\Exporters\AbstractExporter;
use Maatwebsite\Excel\Facades\Excel;

class RechargeOrderExporter extends AbstractExporter
{

    public function export()
    {
        // TODO: Implement export() method.
        $header = [
            '订单id', '系统订单号', '商户订单号', '第三方订单号',
            '商户编号', '订单金额', '充值类型', '结算金额',
            '接口厂商', '是否结算', '代理编号', '代理结算',
            '创建时间', '更新时间'
        ];
        $rows = [];
        $rows[0] = $header;
        $i = 1;
        $data = collect($this->getData());
        foreach ($data->chunk(200) as $chunk) {
            foreach ($chunk as $datum) {
                $row = [
                    $datum['id'],
                    "\t".$datum['plat_no'],
                    "\t".$datum['merchant_no'],
                    "\t".$datum['third_no'],
                    $datum['platuser']['code'],
                    $datum['order_amt'],
                    $datum['payment']['name'],
                    $datum['order_settle'],
                    $datum['upper_if']['name'],
                    $datum['is_settle'] ? '是' : '否',
                    @$datum['proxy_user']['code'],
                    $datum['proxy_settle'],
                    $datum['created_at'],
                    $datum['updated_at']
                ];
                $rows[$i] = $row;
                $i++;
            }
        }
        Excel::create('excel', function ($excel) use ($rows){
            $excel->sheet('sheet', function ($sheet) use ($rows){
                $sheet->setColumnFormat([
                        'A' => '0',
                        'E' => '@',
                        'F' => '0.00',
                        'H' => '0.000000',
                        'L' => '0.000000',
                        'M' => 'yyyy-mm-dd h:mm:ss',
                        'N' => 'yyyy-mm-dd h:mm:ss',
                ]);
                $sheet->rows($rows);
            });
        })->export('xlsx');
    }
}
